package dyl.easycode.test;

import java.io.IOException;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import dyl.easycode.dbutil.DBFactory;
import dyl.easycode.model.Table;
import dyl.easycode.model.TableForm;
import dyl.easycode.util.DbUtil;
import dyl.easycode.util.FreemarkerUtils;
import freemarker.template.Configuration;

public class DBMain {
	public static void main(String[] args) {
		try {
			DBMain dyl  = new DBMain();
			Table t = dyl.getTable();
			dyl.createBean(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void createBean(Table t) throws IOException{
		HashMap<String, Object> root = new HashMap<String, Object>();
		root.put("table", t);
		root.put("sysTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		//是否包含上传
		//root.put("hasUpload","true");
		root.put("hasEdit","true");
		String templatePath = "E:/workSpace/dyl-sys/src/com/easycode/template/layui";
		Configuration cfg = FreemarkerUtils.getConfiguration(templatePath);
		String destPath = "E:/workSpace/dyl-sys/src/com/easycode/test/";
		FreemarkerUtils.createFile(cfg,"javaBean-layui.ftl", root, destPath+t.getClassName()+".java");
		FreemarkerUtils.createFile(cfg,"javaAction-layui.ftl", root, destPath+t.getClassName()+"Action.java");
		FreemarkerUtils.createFile(cfg,"javaServiceImpl-layui.ftl", root, destPath+t.getClassName()+"ServiceImpl.java");
		FreemarkerUtils.createFile(cfg,"viewMain-layui.ftl", root, destPath+t.getJavaProperty()+"Main.jsp");
		FreemarkerUtils.createFile(cfg,"viewForm-layui.ftl", root, destPath+t.getJavaProperty()+"Form.jsp");
		System.out.println("ok");
	}
	public Table getTable() throws Exception{
		TableForm tableForm = new TableForm();
		tableForm.setTableName("sys_quartz");
		tableForm.setPackageName("com.easycode.test");
		Table table = new Table(tableForm);
		Connection con = DbUtil.openConnection("oracle_SHARK");
		//Connection con = DbUtil.openConnection("oracle_paxy");
		//获取注释
		Map<String,String> remarksMap =  DBFactory.getRemarksByOracle(con, table.getTableName());
		//自动构造主键
		/*Column c= new Column("zgh");
		table.addPrimaryKey();
		c.setJavaType("java.lang.String");*/
		//获取列属性
		DBFactory.getColumnList(con,table,remarksMap,"id",null);
	/*	List<Column> columns =  table.getBaseColumns();
		for (int i = 0; i < columns.size(); i++) {
			if(columns.get(i).getColumnName().equalsIgnoreCase("zzmmid")){
				columns.get(i).setSelectSql("select zzmmdm as id,zzmmmc as name from t_zxbz_zzmm order by zzmmdm");
			}
		}*/
		return table;
	}

}
